package ece.pdx.finalproject.roleplay;



import android.os.Bundle;
import android.app.AlertDialog;
import android.support.v4.app.ListFragment;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.view.LayoutInflater;

import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.Toast;

public class FamilyFragment extends ListFragment {
   DataBaseHandler dbHandler;
   ToDoList todo;
   Context context;
   boolean isEdit;
   @Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {

		//View rootView = inflater.inflate(R.layout.list_layout, container, false);
		dbHandler=new DataBaseHandler(this.getActivity());
		
		ToDoAdapter adapter=new ToDoAdapter(this.getActivity(),dbHandler.getFamilyTodos());
		setListAdapter(adapter);
		return super.onCreateView(inflater, container, savedInstanceState);
		}
   

   @Override
	public void onListItemClick(ListView l, View v, int position, long id) {
		// TODO item that was clicked
		context=this.getActivity();
		todo = (ToDoList)getListAdapter().getItem(position);
		AlertDialog alertDialog;
		alertDialog = new AlertDialog.Builder(context).create();
		alertDialog.setTitle("Task Options");
		alertDialog.setMessage("What do you want to do?");
		isEdit=true;
		alertDialog.setButton(AlertDialog.BUTTON_NEUTRAL, "Edit", new DialogInterface.OnClickListener(){

			public void onClick(DialogInterface dialog, int which) {
				Intent intent = new Intent(context, DbAddActivity.class);
				// Start activity
				intent.putExtra("Edit", isEdit);
				intent.putExtra("Id", todo.getId());
				startActivity(intent);	
			}
		});

		alertDialog.setButton(AlertDialog.BUTTON_POSITIVE, "Delete", new DialogInterface.OnClickListener(){

			public void onClick(DialogInterface dialog, int which) {
				dbHandler.deleteTodo(todo.getId());
				setListAdapter(new ToDoAdapter(context, dbHandler.getFamilyTodos()));
				Toast.makeText(context.getApplicationContext(), "Deleted!", Toast.LENGTH_LONG).show();
				
			}
		});

		alertDialog.setButton(AlertDialog.BUTTON_NEGATIVE,"Back", new DialogInterface.OnClickListener(){

			public void onClick(DialogInterface dialog, int which) {
				
			}
		});

		alertDialog.show();
		
	

		
	}

	
}
